import {
  enterpriseNoticeChangeTopUpByIds,
  enterpriseNoticeChangeVisibleTypeByIds,
  enterpriseNoticeCreate,
  enterpriseNoticeDelByIds,
  getEnterpriseNoticeDetail,
  getEnterpriseNoticeList,
  updateEnterpriseNoticeStatusByIds,
} from '@/api/enterprise-notice'
import { ref } from 'vue'

/**
 * 创建企业公告
 */
export const useEnterpriseNoticeCreate = () => {
  // 创建加载
  const createLoading = ref(false)

  // 创建表单
  const form = ref<EnterpriseAPIRequest.Create>({
    content: '',
    coverImage: '',
    id: '',
    name: '',
    status: false,
    topUp: false,
    visibilityType: 1,
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      content: '',
      coverImage: '',
      id: '',
      name: '',
      status: false,
      topUp: false,
      visibilityType: 1,
    }
  }

  /**
   * 创建
   */
  const create = async () => {
    createLoading.value = true
    const res = await enterpriseNoticeCreate(form.value)
    createLoading.value = false
    return res.data
  }

  return {
    form,
    createLoading,
    create,
    resetForm,
  }
}

/**
 * 获取企业公告列表
 */
export const useGetEnterpriseNoticeList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 企业公告列表
  const enterpriseNoticeList = ref<Array<EnterpriseNoticeItem>>([])

  /**
   * 获取列表请求参数
   */
  const getListParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 企业公告列表总条数
  const total = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getEnterpriseNoticeList(getListParam.value)
    total.value = Number(res.data.data.total)
    enterpriseNoticeList.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    total,
    getListParam,
    getListLoading,
    enterpriseNoticeList,
    getList,
  }
}

/**
 * 批量启用企业公告
 */
export const useEnterpriseNoticeEnableByIds = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 启用
   * @param {Array<string>} ids - 企业公告id集合
   */
  const enable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await updateEnterpriseNoticeStatusByIds(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enable,
  }
}

/**
 * 批量禁用企业公告
 */
export const useEnterpriseNoticeDisableByIds = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 禁用
   * @param {Array<string>} ids - 企业公告id集合
   */
  const disable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await updateEnterpriseNoticeStatusByIds(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disable,
  }
}

/**
 * 批量删除企业公告
 */
export const useEnterpriseNoticeDelByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 删除方法
   * @param {Array<string>} ids - 企业公告id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await enterpriseNoticeDelByIds(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 批量置顶企业公告
 */
export const useEnterpriseNoticeTopUpByIds = () => {
  // 置顶加载
  const topUpLoading = ref(false)

  /**
   * 置顶
   * @param {Array<string>} ids - 企业公告id集合
   */
  const topUp = async (ids: Array<string>) => {
    topUpLoading.value = true
    const res = await enterpriseNoticeChangeTopUpByIds(ids, true)
    topUpLoading.value = false
    return res.data
  }

  return {
    topUp,
    topUpLoading,
  }
}

/**
 * 批量取消置顶企业公告
 */
export const useEnterpriseNoticeUnTopUpByIds = () => {
  // 取消置顶加载
  const unTopUpLoading = ref(false)

  /**
   * 取消置顶
   * @param {Array<string>} ids - 企业公告id集合
   */
  const unTopUp = async (ids: Array<string>) => {
    unTopUpLoading.value = true
    const res = await enterpriseNoticeChangeTopUpByIds(ids, false)
    unTopUpLoading.value = false
    return res.data
  }

  return {
    unTopUp,
    unTopUpLoading,
  }
}

/**
 * 批量更新企业公告的可见类型
 */
export const useUpdateEnterpriseNoticeVisibleTypeByIds = () => {
  // 更新可见类型加载
  const updateVisibleTypeLoading = ref(false)

  /**
   * 更新可见类型
   * @param {(1|2|3)} visibleType - 可见类型
   */
  const updateVisibleType = async (ids: Array<string>, visibleType: 1 | 2 | 3) => {
    updateVisibleTypeLoading.value = true
    const res = await enterpriseNoticeChangeVisibleTypeByIds(ids, visibleType)
    updateVisibleTypeLoading.value = false
    return res.data
  }

  return {
    updateVisibleTypeLoading,
    updateVisibleType,
  }
}

/**
 * 获取公告详情
 */
export const useGetEnterpriseNoticeDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 公告详情
  const detail = ref<EnterpriseNoticeDetail>({
    content: '',
    name: '',
    visibilityType: 1,
    status: false,
    topUp: false,
    coverImage: '',
    id: '',
  })

  /**
   * 获取详情
   * @param {String} id - 公告id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getEnterpriseNoticeDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}
